<?php

namespace app\admin\controller;

class Skill extends Base
{
    protected function _infoModule()
    {
        return array(
            'info' => array(
                'name' => '技能库',
                'description' => '技能库模型',
            ),
            'menu' => array(
                array(
                    'name' => '列表',
                    'url' => url('index'),
                    'icon' => 'list',
                ),
            ),
            '_info' => array(
                array(
                    'name' => '添加',
                    'url' => url('info'),
                ),
            ),
        );
    }

    public function index()
    {
        $request = $this->request;

        //筛选条件
        $where = [];
        $pageMaps = [];

        $id = $request->post('id');
        $name = $request->post('name');

        if ($id) {
            $where[] = ['jnid', '=', $id];
        }
        if ($name) {
            $where[] = ['jnname', 'like', '%' . $name . '%'];
        }

        //URL参数
        $pageMaps['id'] = $id;
        $pageMaps['name'] = $name;

        $pageAry = [
            'list_rows' => 20,
            'page' => input('page', 1),
            'path' => 'index',
            'query' => $pageMaps,
        ];

        //查询数据
        $list = model('Skill')->loadList($where, $pageAry);
        if(!empty($list)){
            foreach ($list as $k=>$vo){
                $vo['jndj_name'] = model('Prop')->findValue(['djid'=>$vo['jndj']],'djname');
            }
        }
        $this->assign('list', $list);
        $this->assign('_page', $list->render());
        $this->assign('pageMaps', $pageMaps);

        return $this->fetch('index');
    }

    public function info()
    {
        $request = request();
        $id = input('jnid');
        $model = model('Skill');
        if ($request->method() == 'POST') {
            if ($id) {
                $status = $model->editAll($request);
            } else {
                $status = $model->addall($request);
            }
            if ($status !== false) {
                returnJson(1, '操作成功', ['url' => url('index')]);
            } else {
                returnJson(0, '操作失败');
            }
        } else {
            $info = $model->getInfo($id);
            $this->assign('info', $info);
            //echo "<pre>";print_r($_POST);die;

            $jndj_list = model('Prop')->getList(['djzl'=>1]);
            $this->assign('jndj_list', $jndj_list);

            $this->fetch('info');
        }
    }

    /**
     * 删除
     */
    public function del()
    {
        $request = $this->request;
        $id = $request->post('id');
        if (empty($id)) {
            returnJson(0, '参数不能为空');
        }

        if (model('Skill')->del($id)) {
            returnJson(1, '删除成功！');
        } else {
            returnJson(0, '删除失败');
        }
    }
}

